package com.zhigongtech.core.web.aop;

import java.lang.reflect.UndeclaredThrowableException;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.CredentialsException;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.zhigongtech.common.exception.NomalException;
import com.zhigongtech.core.web.enm.NomalExceptionEnum;
import com.zhigongtech.extension.web.vo.Message;


/**
 * 全局的的异常拦截器（拦截所有的控制器）（带有@RequestMapping注解的方法上都会拦截）
 *
 * @author fengshuonan
 * @date 2016年11月12日 下午3:19:56
 */
@ControllerAdvice
@Order(-1)
public class GlobalExceptionHandler {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    private Message msg = new Message(); 

    /**
     * 拦截业务异常
     */
    @ExceptionHandler(NomalException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public Message notFount(NomalException e) {
        log.error("业务异常:", e);
        return msg.error(e.getCode(), e.getMessage());
    }

    /**
     * 用户未登录异常
     */
    @ExceptionHandler(AuthenticationException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public Message unAuth(AuthenticationException e) {
        log.error("用户未登陆：", e);
        return msg.error(NomalExceptionEnum.USER_NO_LOGIN.getCode(), NomalExceptionEnum.USER_NO_LOGIN.getMessage());
    }

    /**
     * 账号被冻结异常
     */
    @ExceptionHandler(DisabledAccountException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public Message accountLocked(DisabledAccountException e, Model model) {
        return msg.error(NomalExceptionEnum.ACCOUNT_FREEZED.getCode(), NomalExceptionEnum.ACCOUNT_FREEZED.getMessage());
    }
    
    /**
     * 账号密码错误异常
     */
    @ExceptionHandler(CredentialsException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public Message credentials(CredentialsException e, Model model) {
        return msg.error(NomalExceptionEnum.USER_PWD_ERROR.getCode(), NomalExceptionEnum.USER_PWD_ERROR.getMessage());
    }

//    /**
//     * 验证码错误异常
//     */
//    @ExceptionHandler(InvalidKaptchaException.class)
//    @ResponseStatus(HttpStatus.BAD_REQUEST)
//    public String credentials(InvalidKaptchaException e, Model model) {
//        String username = getRequest().getParameter("username");
//        model.addAttribute("tips", LocaleStringUtil.getMessage("GlobalExceptionHandler.credentials.InvalidKaptcha"));
//        return "/login.html";
//    }

    /**
     * 无权访问该资源异常
     */
    @ExceptionHandler(UndeclaredThrowableException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public Message credentials(UndeclaredThrowableException e) {
        log.error("权限异常!", e);
        return msg.error(NomalExceptionEnum.NO_PERMITION.getCode(), NomalExceptionEnum.NO_PERMITION.getMessage());
    }
    /**
     * 
     * @Title: credentials   
     * @Description: 接口访问，对应参数不匹配 
     * by Alan 2019-05-30 17:54:49
     * @param e
     * @return      
     * @return  Message
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public Message credentials(HttpMessageNotReadableException e) {
    	log.error("权限异常!", e);
    	return msg.error(NomalExceptionEnum.REQUEST_INVALIDATE.getCode(), NomalExceptionEnum.REQUEST_INVALIDATE.getMessage());
    }
    /**
     * 
     * @Title: credentials   
     * @Description: 权限异常 @requestpermission 注解不匹配 
     * by Alan 2019-05-30 17:53:35
     * @param e
     * @return      
     * @return  Message
     */
    @ExceptionHandler(UnauthenticatedException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ResponseBody
    public Message credentials(UnauthenticatedException e) {
        log.error("权限异常!", e);
        return msg.error(NomalExceptionEnum.NO_PERMITION.getCode(), NomalExceptionEnum.NO_PERMITION.getMessage());
    }
    
    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public Message notFount(RuntimeException e) {
        log.error("运行时异常:", e);
        return msg.error(NomalExceptionEnum.SERVER_ERROR.getCode(), NomalExceptionEnum.SERVER_ERROR.getMessage()+" : "+e);
    }
}
